package com.scm.interceptor;

import com.scm.pojo.UserModel;
import com.scm.service.UserModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PowerInterceptor
 * @Description TODO
 * @Author Xu
 * @Date 2021/7/6 0:44
 * @Version 1.0
 **/
public class PowerInterceptor implements HandlerInterceptor {
    @Autowired
    private UserModelService userModelService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String account = request.getSession().getAttribute("account").toString();
        List<String> list = new ArrayList<>();
        userModelService.getUserModel(account).forEach(userModel -> list.add(userModel.getModelName()));
        String url = request.getRequestURI();
        if (list.contains("admin")){
            return true;
        }
        for (int i = 0;i<=list.size();i++){
            if (url.contains(list.get(i))){
                return true;
            }
        }
        return false;
    }
}
